<template>
  <div class="divBox">
    <div class="header clearfix">
      <div class="container">
        <el-form inline>
          <el-form-item label="商品分类：">
            <el-cascader v-model="tableFrom.cateId" :options="merCateList" :props="props" clearable class="selWidth mr20" @change="getProductList(1)" />
          </el-form-item>
          <el-form-item label="商品搜索：">
            <el-input v-model="tableFrom.keywords" placeholder="请输入商品名称，关键字，产品编号" class="search-select selWidth" @input="onInput($event)">
              <el-button slot="append" icon="el-icon-search" @click="getProductList('1')" />
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="productLoading"
      :data="productList"
      size="mini"
      style="width: 100%"
    >
      <el-table-column label="#" width="55">
        <template slot-scope="{ row }">
          <el-radio v-model="templateRadio" @change="getChangeRow(row)">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" min-width="50" />
      <el-table-column label="商品图" width="80">
        <template slot-scope="{ row }">
          <div class="demo-image__preview">
            <div v-viewer class="pictrue">
              <img v-lazy="$uploadURL(row.image)" :alt="row.title" class="mr10" style="width: 36px;height: 36px;">
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="商品名称" min-width="180" />
      <el-table-column label="商品分类" min-width="150">
        <template slot-scope="{ row }">
          <span v-for="(item, index) in $util.splitArray(row.cateStr, '/')" :key="index" class="mr10">{{ $util.splitArray(row.cateStr, '/').length - 1 > index ? item + ',' : item }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="productBox">
      <pagination
        v-show="pagination.total > 0"
        :total="pagination.total"
        :page.sync="pagination.page"
        :limit.sync="pagination.limit"
        :page-sizes="[10, 20, 30, 50]"
        @pagination="productChange"
      />
    </div>
  </div>
</template>

<script>
import {
  categoryApi,
  productListApi
} from '@/api/store'

export default {
  name: 'Index',
  data() {
    return {
      productLoading: false,
      tableFrom: {
        cateId: 0,
        keywords: ''
      },
      merCateList: [],
      props: {
        children: 'children',
        label: 'name',
        value: 'id',
        emitPath: false
      },
      templateRadio: '',
      productList: [],
      pagination: {
        page: 1,
        limit: 10,
        total: 0
      }
    }
  },
  mounted() {
    this.getProductList()
    this.getCategorySelect()
  },
  methods: {
    productChange(reply) {
      this.$set(this.pagination, 'page', reply.page)
      this.$set(this.pagination, 'limit', reply.limit)
      this.getProductList()
    },
    getChangeRow(row) {
      this.$emit('getChangeRowEmit', row)
    },
    getProductList(num) {
      this.$set(this, 'productLoading', true)
      productListApi({
        pageNumber: num || this.pagination.page,
        pageSize: this.pagination.limit,
        cateId: this.tableFrom.cateId,
        keyword: this.tableFrom.keywords,
        orderby: 'CreateDateUtc desc'
      }).then(res => {
        this.$set(this, 'productList', res.data)
        this.$set(this.pagination, 'total', res.head.totalCount)
        this.$set(this, 'productLoading', false)
        console.log(res)
      }).catch(() => {
        this.$set(this, 'productList', [])
        this.$set(this.pagination, 'total', 0)
        this.$set(this, 'productLoading', false)
      });
    },
    // 商户分类
    getCategorySelect() {
      categoryApi({ isShow: 1 }).then(res => {
        this.merCateList = res
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .demo-image__preview img {
    cursor: pointer;
  }
</style>
